#region MIT
/*
Engine Of Evermore (http://code.google.com/p/engineofevermore/)
Copyright (c) 2008, 2009 Michael Woerister

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#endregion

using System;
using System.IO;
using System.Collections.Generic;
using System.Xml.Linq;
using Blue.ResourceManager;
using Green.Logging;
using EngineOfEvermore.Common;


namespace EngineOfEvermore.Scene.ObjectLibrary
{
	
	public class SceneGraphContentResource : Resource
	{
		private IList<SceneObject> _sceneObjects;
		
		
		public IEnumerable<SceneObject> SceneObjects
		{
			get { return _sceneObjects; }
		}
		
				
		
		public SceneGraphContentResource( string resourceName ) : base( resourceName )
		{
			_sceneObjects = new List<SceneObject>();
		}
		
		
		
		public void AddSceneObject( SceneObject obj )
		{
			_sceneObjects.Add( obj );
		}
		
		
		
		public void RemoveSceneObject( SceneObject obj )
		{
			_sceneObjects.Remove( obj );
		}
		
		
		
		public void AddContentToSceneGraph( SceneGraph scene )
		{
			foreach( SceneObject o in _sceneObjects ) {
				scene.AddSceneObject( o );
			}
		}
		
		
		
		public static void SaveContentAndReplace( string resourceName, SceneGraph scene )
		{
			_saveContent( resourceName, scene, true );
		}
		
		
		
		public static void SaveContent( string resourceName, SceneGraph scene )
		{
			_saveContent( resourceName, scene, false );
		}
		
		
		
		private static void _saveContent( string resourceName, SceneGraph scene, bool replace )
		{
			XmlSerialization serializer = new XmlSerialization();
			serializer.AddObject( scene );
			serializer.Save( resourceName, replace ); 
		}		    
		
		
		
		public static SceneGraphContentResource LoadResource ( string resourceName, Stream stream ) 
		{
			XmlSerialization serializer = new XmlSerialization( resourceName, stream );
			
			int count = serializer.GetObjectCount();			
			if ( count == 0 ) {
				Log.Error( "could not load SceneGraph content. No SceneObjectList found." );
				
			} else if ( count > 1 ) {
				Log.Error( "coult not load SceneGraph content. More than one SceneObjectList defined, which is currently not supported." );
				
			} else {
				return serializer.GetObject<SceneGraphContentResource>( 0 );
			}
			
			return null;
		}
	}

	
	
	
	/// <summary>
	/// SceneGraphContentXmlSerializer
	/// 
	/// This is currently the SceneGraph serializer class. The deserializer returns a 
	/// SceneGraphContentRecource not a SceneGraph. This is because we can not add SceneObjects 
	/// to an uninitialized SceneGraph.
	/// </summary>
	class SceneGraphContentXmlSerializer : IXmlObjectSerializer
	{		
		private SceneGraph _obj = null;
		
		public Object Obj {
			get {
				return ( Object ) _obj;
			}
			
			set {
				if ( value is SceneGraph )
					_obj = ( SceneGraph ) value;
				else
					_obj = null;
			}
		}

		
		public void getXml( XElement node, XmlSerialization serializer ) 
		{
			XElement objectList = new XElement( "SceneObjectList" );
			
			foreach( SceneObject o in _obj.SceneObjects ) {
				serializer.getXmlFromObject( o, objectList );	
			}
			
			node.Add( objectList );
		}
			
		
		public Object loadFromXml( XElement node, XmlSerialization serializer )
		{
			SceneGraphContentResource content = new SceneGraphContentResource( serializer.CurrentResourceName );
			
			foreach( XElement cur in node.Element( "SceneObjectList" ).Elements() ) {
				SceneObject s = serializer.loadObjectFromXml<SceneObject>( cur );	
				if ( s != null )
					content.AddSceneObject( s );
				else	
					Log.Error( "could not load one SceneObject" );
			}
			
			return content;	
		}
	
	}
}
